
import { Sglt } from "./SgltDefine";
import { CommonSystem } from "./systems/CommonSystem";
import { Comp } from "./CompDef";
import { UiSystem } from "./systems/UiSystem";
import { MoveSystem } from "./systems/MoveSystem";
import { GpCmdSystem } from "./systems/CmdSystem";
import { BattleSystem } from "./systems/battle/BattleSystem";
import { HeroSystem } from "./systems/HeroSystem";
import { BulletSystem } from "./systems/battle/BulletSystem";
import { ObjPool } from "../../common/ObjPool";
import Agent from "../../common/RVO/Agent";
import { BuildingSystem } from "./systems/BuildingSystem";
import { BuffSystem } from "./systems/battle/BuffSystem";
import { RandomPseudo } from "../../../../extensions/cocos-scaffold-3x/assets/common/RandomPseudo";
import ecs, { EcsComponent, EcsEntity, EcsEntityGroup} from "ecs-ts";



export class GpEcsWorld extends ecs.EcsWorld {
    random: RandomPseudo
    protected initSingletons(): void {
        this.addSingleton(new Sglt.WorldInfo(2338))
        // this.addSingleton(new Sglt.WorldInfo(Math.floor(Math.random() * 9999)))
        this.addSingleton(new Sglt.CommandInfo())
        this.addSingleton(new Sglt.UiInfo())
        console.log("initSingletons",this.getSingleton(Sglt.WorldInfo).seed);
        
    }
    protected initEntityTypes(): void {
    }
    protected initSystems(): void {
        this.addSystem(new CommonSystem(this))
        this.addSystem(new MoveSystem(this))
        this.addSystem(new HeroSystem(this))
        this.addSystem(new BuildingSystem(this))

        this.addSystem(new BattleSystem(this))
        this.addSystem(new BuffSystem(this))

        this.addSystem(new BulletSystem(this))
        this.addSystem(new GpCmdSystem(this, []))
        this.addSystem(new UiSystem(this, []))
    }

    public removeEntity(id: number): void {
        const entity = this.getEntity(id)
        if (!entity) {
            return
        }
        super.removeEntity(id)
    }



    afterEntityCreated(entity: EcsEntity) {
        this.getSingleton(Sglt.UiInfo).afterEntityCreated(entity)
    }
    beforeEntityRemove(entity: EcsEntity) {
        this.getSingleton(Sglt.UiInfo).beforeEntityRemove(entity)
    }
    async init() {
        const worldInfo = this.getSingleton(Sglt.WorldInfo)
        this.random = new RandomPseudo(worldInfo.seed, worldInfo)

        // import('@dimforge/rapier2d').then((module) => {console.log("xxxxxxxxxxxxxxx",module);
        // })
        // console.log("zzzzzzzzzz",this.RAPIER,RRR);

        // this.addSingleton(new Sglt.RapierInfo(this.RAPIER))
        this.enable = true
    }
}